package com.project.frba.bifront.server.rpc;

import java.io.IOException;
import java.util.GregorianCalendar;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.project.frba.bi.BiUtil;
import com.project.frba.bi.XLSExporter;
import com.project.frba.bi.descriptor.Descriptor;
import com.project.frba.bi.descriptor.DescriptorsLoader;
import com.project.frba.bifront.client.model.Cube;
import com.project.frba.bifront.client.rpc.ExcelServices;

public class ExcelServicesImpl extends RemoteServiceServlet implements ExcelServices {
	
	public String exportToExcel(String descriptorName, Cube cube){
		Descriptor descriptor = DescriptorsLoader.getDescriptorByName(descriptorName);
		XLSExporter exporter = new XLSExporter(descriptor);
		String fileName = generateXLSFilename(descriptorName);
		try {
			exporter.export(BiUtil.getRepositoryPath() + fileName, cube);
		} catch (IOException e) {
			return " ";
		}
		return fileName;
	}
	
	public String generateXLSFilename(String descriptorName){
		GregorianCalendar calendar = new GregorianCalendar();
		String fileName = descriptorName + 
				calendar.get(GregorianCalendar.YEAR) + 
				calendar.get(GregorianCalendar.MONTH + 1) + 
				calendar.get(GregorianCalendar.DAY_OF_MONTH) +
				calendar.getTimeInMillis() + ".xls";
		return fileName;
	}
}
